package com.hitgo.soccer.config;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ConfigUtil
{
	public static Element GetRootElement(String strFileName)
	{
		if(strFileName == null || strFileName.isEmpty())
			return null;
		
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder;
        Document document = null;
		try 
		{
			builder = builderFactory.newDocumentBuilder();
			File reader = new File(strFileName);
	        document = builder.parse(reader);  
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			return null;
		}          

        return document.getDocumentElement();
	}
	
	public static String GetAttributeStrValue(Node node, String strName)
	{
		Node node1 = node.getAttributes().getNamedItem(strName); 
		if(node1 != null)
			return node1.getNodeValue();

		return null;
	}
	
	public static int GetAttributeIntValue(Node node, String strName, int nBase)
	{
		int value = 0;
		
		try
		{
			String strValue = GetAttributeStrValue(node, strName);
			if(strValue != null && !strValue.isEmpty())
				value = Integer.parseInt(strValue, nBase);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return value;
	}
	public static int GetAttributeIntValue(Node node, String strName)
	{
		return GetAttributeIntValue(node, strName, 10);
	}
}
